#!/bin/sh

echo "=====================\033[35mMesh Optimization\033[0m=========================="
echo "=                       Main Steps:                            ="
echo "=     1. Poisson Reconstruction(fill holes)                    ="
echo "=     2. open3d-python simplification                          ="
echo "=     3. meshlabserver handling(change a dtype for next step)  ="
# echo "=     4. plane-rgbd-opt(plane simplification & texturing)      ="
echo "=                                                              ="
echo "=               If bug happens, just PRAY.                     ="
echo "================================================================"

### PLY filename
PLYNAME="YOUR PLY NAME"

### Working directory contains the input PLY model. All output files will be put inside this directory.
WORKINGDIR=/tmp/

### This is the original RGB-D data including color images, depth images and camera pose files
# RGBD="/tmp/rgbd/RealShot201224_1"

# echo "rgbd&pose PATH: " "\033[32m $RGBD \033[0m"

echo "ply file PATH: " " \033[32m ${WORKINGDIR}/${PLYNAME}.ply \033[0m" 

# STEP 1: poisson reconstruction
echo "================================================================"
echo "\033[33mSTEP 1: poisson reconstruction \033[0m"
echo "================================================================"

/bin/PoissonRecon --in ${WORKINGDIR}/${PLYNAME}.ply --out ${WORKINGDIR}/${PLYNAME}_PR.ply --depth 10 --pointWeight 0

if [ -f "${WORKINGDIR}/${PLYNAME}_PR.ply" ]; then 
    echo "save Poisson Reconstruction result at \033[32m${WORKINGDIR}/${PLYNAME}_PR.ply\033[0m"
else
    echo "\033[31mpoisson reconstruction failed. exit\033[0m"
    exit 1
fi

# STEP 2: open3d-python simplification
echo "================================================================"
echo "\033[33mSTEP 2: open3d-python simplification \033[0m"
echo "================================================================"


python3 /home/meshComplete.py ${WORKINGDIR}/${PLYNAME}_PR.ply ${WORKINGDIR}/${PLYNAME}_PR_out1.ply

if [ -f "${WORKINGDIR}/${PLYNAME}_PR_out1.ply" ]; then 
    echo "save oped3d simplification result at \033[32m${WORKINGDIR}/${PLYNAME}_PR_out1.ply\033[0m"
else
    echo "\033[31mopen3d simplification failed. exit\033[0m"
    exit 1
fi

# STEP 3: meshlabserver handling
echo "================================================================"
echo "\033[33mSTEP 3: meshlabserver handling \033[0m"
echo "================================================================"


meshlabserver -i ${WORKINGDIR}/${PLYNAME}"_PR_out1.ply" -o ${WORKINGDIR}/${PLYNAME}"_PR_out2.ply" -om vc

if [ -f "${WORKINGDIR}/${PLYNAME}_PR_out2.ply" ]; then 
    echo "save meshlabserver handling result at \033[32m${WORKINGDIR}/${PLYNAME}_PR_out2.ply\033[0m"
else
    echo "\033[31mmeshlabserver handling failed. exit\033[0m"
    exit 1
fi

# # STEP 4: plane-rgbd-opt
# echo "================================================================"
# echo "\033[33mSTEP 4: plane-rgbd-opt \033[0m"
# echo "================================================================"


# ### 0 for BundleFusion/3DLite data, 1 for ICL-NUIM data (image type, camera pose details are different)
# DATATYPE=0

# ### Target cluster/plane number in mesh partition
# CLUSTERNUM=2000

# ### Start and end frame index
# START=0

# END=7000

# ROOTPATH="/home/plane-opt-rgbd"
# # CODEPATH=$ROOTPATH/bin
# # cd $WORKINGDIR

# ## Mesh partition and simplification
# # mesh_partition output_ini.ply output_ini.txt output.ply output.txt
# echo ---------------------------------------
# /bin/mesh_partition ${WORKINGDIR}/${PLYNAME}"_PR_out2"".ply" $CLUSTERNUM ${WORKINGDIR}/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM"_ini.ply" ${WORKINGDIR}/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM"_ini.txt"
# # $CODEPATH/mesh_partition $PLYNAME"_c"$CLUSTERNUM"_ini.ply" $PLYNAME"_c"$CLUSTERNUM"_ini.txt" $PLYNAME"_c"$CLUSTERNUM".ply" $PLYNAME"_c"$CLUSTERNUM".txt"
# # For debug only
# # $CODEPATH/mesh_partition --run_post_processing=false cluster2000_simp.ply cluster2000_simp.txt
# #
# # Run again on the last output to better simplify the mesh. This will be fast.
# # NOTE: simplification_border_edge_ratio is the target percentage of border edges left after simplification.

# if [ -f ${WORKINGDIR}/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM"_ini.ply" ] && [ -f ${WORKINGDIR}/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM"_ini.txt" ] ; then 
#     echo "save mesh partition result at \033[32m${WORKINGDIR}/${PLYNAME}_PR_out2_c${CLUSTERNUM}_ini.ply\033[0m"
# else
#     echo "\033[31mmesh partition failed. exit\033[0m"
#     exit 1
# fi

# echo ---------------------------------------
# /bin/mesh_partition --run_post_processing=false --simplification_border_edge_ratio=0.1 ${WORKINGDIR}/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM"_ini.ply" ${WORKINGDIR}/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM"_ini.txt" $WORKINGDIR/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM".ply" $WORKINGDIR/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM".txt"

# ## Reset filenames of ICL-NUIM data to fit the input
# if [ $DATATYPE = 1 ]; then
#     echo ---------------------------------------
#     echo Resetting filenames for ICL-NUIM dataset
# 	for i in  $(seq $START $END)
# 	do
#         printf -v j "%06d" $i # NOTE to run by 'bash ./run_linux.sh' instead of 'sh' command, otherwise there would be error about the -v option
# 		mv $RGBD/rgb/$i.png $RGBD/frame-$j.color.png
# 		mv $RGBD/depth/$i.png $RGBD/frame-$j.depth.png
#         # NOTE: you need to pre-put all poses files (filename like 1.pose.txt) into the RGBD folder
#         mv $RGBD/$i.pose.txt $RGBD/frame-$j.pose.txt
# 	done
#     rm -rf $RGBD/rgb
#     rm -rf $RGBD/depth
#     # NO info.txt file in ICL-NUIM data so we provide one
# 	cp $ROOTPATH/ICL-NUIM-info.txt $RGBD/info.txt
# fi

# ## Mesh visibility
# echo ---------------------------------------
# # Create a new folder 'visibility' to store visibility files
# if [ ! -d "visibility" ]; then
# 	mkdir ${WORKINGDIR}/"visibility"
# fi

# cd ${WORKINGDIR}

# cp $ROOTPATH/mesh_visibility/*.vert ${WORKINGDIR}
# cp $ROOTPATH/mesh_visibility/*.frag ${WORKINGDIR}
# MESA_GL_VERSION_OVERRIDE=4.1 MESA_GLSL_VERSION_OVERRIDE=410 /bin/mesh_visibility -a $WORKINGDIR/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM".ply" $RGBD ${WORKINGDIR}/"visibility" $START $END
# rm ${WORKINGDIR}/*.vert
# rm ${WORKINGDIR}/*.frag 

# if [ -f $WORKINGDIR/${PLYNAME}"_PR_out2""_c"$CLUSTERNUM".ply" ] ; then 
#     echo "save mesh visibility result at \033[32m $WORKINGDIR/${PLYNAME}_PR_out2_c${CLUSTERNUM}.ply\033[0m"
# else
#     echo "\033[31mmesh visibility failed. exit\033[0m"
#     exit 1
# fi

# ## Image blur estimation
# echo ---------------------------------------
# if [ $DATATYPE = 0 ]; then
#     /bin/blur_estimation $RGBD $START $END ${WORKINGDIR}/blur.txt
# elif [ $DATATYPE = 1 ]; then
#     /bin/blur_estimation $RGBD $START $END ${WORKINGDIR}/blur.txt frame- .color.png 6
# fi

# ## Texture and Geometry Optimization
# echo ---------------------------------------
# # $CODEPATH/mesh_texture_opt $PLYNAME"_c"$CLUSTERNUM".ply" $PLYNAME"_c"$CLUSTERNUM".txt" blur.txt $RGBD visibility $DATATYPE $START $END $PLYNAME"_c"$CLUSTERNUM".obj"
# # $CODEPATH/mesh_texture_opt --use_opt_pose=false --closest_pose_translation=0.03 --closest_pose_rotation_angle=0.02 $PLYNAME"_c"$CLUSTERNUM".ply" $PLYNAME"_c"$CLUSTERNUM".txt" blur.txt $RGBD visibility $DATATYPE $START $END $PLYNAME"_c"$CLUSTERNUM"_ini.obj"
# /bin/mesh_texture_opt --plane_opt_loop_number=0 --closest_pose_translation=0.03 --closest_pose_rotation_angle=0.02 $WORKINGDIR/${PLYNAME}"_PR_out2_c"$CLUSTERNUM".ply" $WORKINGDIR/${PLYNAME}"_PR_out2_c"$CLUSTERNUM".txt" blur.txt $RGBD visibility $DATATYPE $START $END $WORKINGDIR/${PLYNAME}"_PR_out2_c"$CLUSTERNUM"_ini.obj"

# if [ -f $WORKINGDIR/${PLYNAME}"_PR_out2_c"$CLUSTERNUM"_ini.obj" ] ; then 
#     echo "save mesh texture optimization result at \033[32m ${WORKINGDIR}/${PLYNAME}_PR_out2_c${CLUSTERNUM}_ini.obj\033[0m"
# else
#     echo "\033[31mmesh texture optimization failed. exit\033[0m"
#     exit 1
# fi